package org.fall.mvc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.entity.Admin;
import org.fall.constant.CrowdConstant;
import org.fall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

import java.util.List;

import static org.fall.constant.CrowdConstant.LOGIN_ADMIN_NAME;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
/**
 * 退出系统
 */
    @RequestMapping(value ="admin/do/logout.do")
    public String doLogout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/login/page.html";
    }
    /**
     *管理员登录
     * @param loginUser
     * @param loginPwd
     * @param session
     * @return
     */
    @RequestMapping(value = "admin/login/doLogin.html")
    public String doLogin(@RequestParam("login-user") String loginUser,
                          @RequestParam("login-pwd") String loginPwd,
                          HttpSession session){
        // 1.调用service方法执行登录检查
        // 返回admin对象说明登录成功，否则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginUser,loginPwd);
        //将admin存session
        // 2.将登录成功返回的admin对象存入Session域
        session.setAttribute(LOGIN_ADMIN_NAME,admin);
        //重定向到登录完成后的主页面（重定向防止重复提交表单，增加不必要的数据库访问）
        return "redirect:/admin/main/page.html";
    }

    /**
     * 管理员列表
     * @param page
     * @param keyword
     * @return
     */
//    @PreAuthorize("hasRole('CEO')")
    @RequestMapping(value = "/admin/page/page.html")
    public String getAdminPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "keyword", defaultValue = "") String keyword,
                               ModelMap modelMap){
        //一页显示10份信息
        PageHelper.startPage(page,10);
        List<Admin> admins = adminService.getPageInfoByKeyword(keyword);
        //一次只显示5个页面选项
        PageInfo pageInfo = new PageInfo(admins,5);
        //将得到的PageInfo存入modelMap，传给前端
        modelMap.addAttribute(CrowdConstant.NAME_PAGE_INFO,pageInfo);
        return  "admin-page";
    }
    @PreAuthorize("hasRole('CEO')")
    @RequestMapping(value = "/admin/page/remove/{adminId}/{page}/{keyword}.html")
    public String removeAdmin(@PathVariable(value = "adminId") Integer adminId,
                              @PathVariable(value = "keyword") String keyword,
                              @PathVariable(value = "page") Integer page){
        adminService.removeAdmin(adminId);
        return "redirect:/admin/page/page.html?page="+page+"&keyword="+keyword;
    }

    /**
     *新增管理员
     * @param admin
     * @return
     */
    @PreAuthorize("hasAuthority('admin:save')")
    @RequestMapping(value = "admin/page/doSave.html")
    public String saveAdmin(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/page/page.html?page="+Integer.MAX_VALUE;
    }

    /**
     * 返回数据到修改界面
     * @param adminId
     * @return
     */
    @RequestMapping(value = "/admin/page/update/{adminId}/{page}/{keyword}.html")
    public String getAdminById(@PathVariable(value = "adminId") Integer adminId,
                               @PathVariable (value = "page") Integer page,
                               @PathVariable (value = "keyword") String keyword,
                               ModelMap modelMap){
        Admin admin = adminService.getAllByAdminId(adminId);
        modelMap.addAttribute("admin",admin);
        modelMap.addAttribute("keyword",keyword);
        modelMap.addAttribute("page",page);
        return "admin-update";
    }
    @RequestMapping(value = "admin/page/doUpdate.html")
    public String update(Admin admin,
                         @RequestParam(value = "keyword") String keyword,
                         @RequestParam(value = "page") Integer page){
        adminService.update(admin);
        //重定向到管理员当前页面
        return "redirect:/admin/page/page.html?page="+page+"&keyword="+keyword;
    }
}
